package com.controller.after;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.controller.base.BaseController;
import com.entity.Attachment;
import com.entity.ClassiFication;
import com.entity.LoginTable;
import com.entity.UserIndustry;
import com.entity.UserIndustryRelations;
import com.entity.Users;
import com.service.AttaRelationshipService;
import com.service.AttachmentService;
import com.service.ClassiFicationService;
import com.service.IndRelationsService;
import com.service.IndustryService;
import com.service.UsersService;
@Controller
@RequestMapping("/Use/")
//用户控制器
public class UseController extends BaseController<Object>{
	//用户
	@Autowired
	private UsersService useService;
	//用户分类
	@Autowired 
	private ClassiFicationService FicationService;
	//用户行业
	@Autowired
	private IndustryService IndService;
	//行业
	@Autowired
	private IndRelationsService  IndReServlce;
	//附件关系
	@Autowired
	private AttaRelationshipService AttaRelaService;
	//附件
	@Autowired
	private AttachmentService fjService;
	
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(Users o,Page<Users> page)throws Exception{
		AjaxRes res=new AjaxRes();
		Page<Users> indPages =useService.findByPage(o, page);
		res.setSucceed(indPages, "数据获取成功");
		return res;
	}

	
	
	
	//转到添加页面
	@RequestMapping("/Show")
	@ResponseBody
	public ModelAndView Show(){
		ModelAndView mv =  new ModelAndView();
		mv.addObject("state", "1");
		mv.setViewName("pages/Users/AddUser");
		return mv;
	}
	
	//转到修改页面
	@RequestMapping("/ShowUpdate")
	@ResponseBody
	public ModelAndView ShowUpdate(Users user){

		System.out.println("转到修改页面"+user);
		if(user.getUsersName()!=null){
			String uid[] =user.getUsersId().split(",");
			System.out.println(uid);
			for(int i=0;i<uid.length;i++){
				user.setUsersId(uid[0]);
			}
		}
		Attachment att=new Attachment();
		att.setAssociatedName("newsInformation");
		ModelAndView mv =  new ModelAndView();
		List<Users> UsList=useService.find(user);
		mv.addObject("UsList", UsList.get(0));
		mv.addObject("state", "2");
		mv.setViewName("pages/Users/UpdateUsers");
		return mv;
	}
	
	//查询用户附件转到修改页面
	@RequestMapping("/ShowAtt")
	@ResponseBody
	public AjaxRes ShowAtt(Users user,Page<Attachment> page){
		AjaxRes res=new AjaxRes();
		Page<Attachment> indPages =fjService.findByPageId(new Attachment(), page,"Users",user.getUsersId());
		res.setSucceed(indPages, "数据获取成功");
		return res;
	}

	//查询用户分类
	@Transactional
	@RequestMapping("/findCf")
	@ResponseBody
	public AjaxRes findCf(){
		AjaxRes res=new AjaxRes();
		Page<ClassiFication> page=new Page<>();
		Page<ClassiFication> Cflist=FicationService.findByPage(new ClassiFication(), page);
		res.setSucceed(Cflist,"数据获取成功");
		return res;	
	}

	//查询用户行业
	@RequestMapping("/findIndustry")
	@ResponseBody
	public AjaxRes findIndustry(){
		AjaxRes res=new AjaxRes();
		Page<UserIndustry> page=new Page<>();
		Page<UserIndustry> PaIndList= IndService.findByPage(new UserIndustry(), page);
		res.setSucceed(PaIndList,"数据获取成功");
		return res;	
	}
	//添加用户和修改用户
	@RequestMapping("/UpdateOrAdd")
	@ResponseBody
	public AjaxRes UpdateOrAdd(Users user,UserIndustryRelations Uir,String AttachmentId,LoginTable login)throws Exception{
		AjaxRes res=new AjaxRes();
		if(user.getUsersId()!=null && !user.getUsersId().equals("")){
			try {
				//修改用户
				useService.updateAll(user,Uir,AttachmentId);
				res.setResMsg("修改成功");
			} catch (Exception e) {
				res.setResMsg("修改失败");
			}
		}else{
			try {
				System.out.println("添加用户");
				//保存用户
				useService.insterFj(user, AttachmentId,Uir,login);
				//保存用户行业关系表
				res.setResMsg("添加成功");
			} catch (Exception e) {
				res.setResMsg("添加失败");
			}
		}
		return res;
	}
	
	
	
	
	@RequestMapping("/Delel")
	@ResponseBody
	public ModelAndView Delel(Users user,UserIndustryRelations Uir,String idList)throws Exception{
		ModelAndView mv =  new ModelAndView();
		if(user.getUsersId()!=null){
			useService.delete(user);
			IndReServlce.delete(Uir);
		}
		if(idList!=null){
			List<String>  gameids= java.util.Arrays.asList(idList.split(","));
			List<Users> UserList=new ArrayList<Users>();
			List<UserIndustryRelations> UidtryList=new ArrayList<UserIndustryRelations>();
			for(int i=0;i<gameids.size();i++){
				Users Uo=new Users();
				UserIndustryRelations Uidtry=new UserIndustryRelations();
				Uo.setUsersId(gameids.get(i));
				Uidtry.setUsersId(gameids.get(i));;
				UidtryList.add(Uidtry);
				UserList.add(Uo);
			}
			useService.deleteBatch(UserList);
			IndReServlce.deleteBatch(UidtryList);
		}
		mv.setViewName("pages/Users/UserContent");
		return mv;
	}
	
}
